import { fileURLToPath, URL } from 'node:url';
import path from 'path';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueDevTools from 'vite-plugin-vue-devtools';

// https://vite.dev/config/
export default defineConfig({
	base: './',
	plugins: [vue()],
	resolve: {
		alias: {
			'@': path.resolve(__dirname, './src'),
		},
	},
	// 添加静态资源处理
	build: {
		assetsDir: 'assets',
		rollupOptions: {
			input: {
				main: path.resolve(__dirname, 'index.html'),
			},
			output: {
				assetFileNames: (assetInfo) => {
					const info = assetInfo.name.split('.');
					let extType = info[info.length - 1];
					if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)) {
						extType = 'media';
					} else if (/\.(png|jpe?g|gif|svg|ico|webp)(\?.*)?$/i.test(assetInfo.name)) {
						extType = 'imgs';
					} else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
						extType = 'fonts';
					} else if (/\.(css)(\?.*)?$/i.test(assetInfo.name)) {
						extType = 'css';
					} else if (/\.(js)(\?.*)?$/i.test(assetInfo.name)) {
						extType = 'js';
					}
					return `assets/${extType}/[name]-[hash][extname]`;
				},
				chunkFileNames: 'assets/js/[name]-[hash].js',
				entryFileNames: 'assets/js/[name]-[hash].js',
			},
		},
		assetsInlineLimit: 4096,
		chunkSizeWarningLimit: 500,
		copyPublicDir: true,
	},
	// 添加对外部依赖的处理
	optimizeDeps: {
		include: ['leaflet', 'proj4', 'leaflet.fullscreen'],
		// 强制预构建这些插件
		entries: ['src/assets/plug/*.js', 'src/assets/plug/**/*.js'],
	},
	server: {
		host: '0.0.0.0', // 监听所有地址
		port: 5173, // 指定端口号
	},
});
